package com.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;

public class HashFile {

	/**
	 * @param args
	 */
	public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static void main(String[] args) {

		String fileName = "D:\\1";
		File file = new File(fileName);
		String hashType = "MD5";
		try {
			System.out.println(hashType + " == " + getHash(fileName, hashType));
			System.out.println(hashType + " == " + getHash(file, hashType));
			hashType = "SHA1";
			System.out.println(hashType + " == " + getHash(fileName, hashType));
			System.out.println(hashType + " == " + getHash(file, hashType));
			hashType = "SHA-256";
			System.out.println(hashType + " == " + getHash(fileName, hashType));
			System.out.println(hashType + " == " + getHash(file, hashType));
			hashType = "SHA-384";
			System.out.println(hashType + " == " + getHash(fileName, hashType));
			System.out.println(hashType + " == " + getHash(file, hashType));
			hashType = "SHA-512";
			System.out.println(hashType + " == " + getHash(fileName, hashType));
			System.out.println(hashType + " == " + getHash(file, hashType));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String getHash(String fileName, String hashType) {
		InputStream fis = null;
		try {
			fis = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		byte[] buffer = new byte[1024];
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance(hashType);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		int numRead = 0;
		try {
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return toHexString(md5.digest());
	}

	public static String getHash(InputStream file, String hashType) {
		InputStream fis = null;
		fis = file;
		byte[] buffer = new byte[1024];
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance(hashType);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		int numRead = 0;
		try {
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return toHexString(md5.digest());
	}

	public static String getHash(File fileName, String hashType) {
		InputStream fis = null;
		try {
			fis = new FileInputStream(fileName);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		byte[] buffer = new byte[1024];
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance(hashType);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		int numRead = 0;
		try {
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return toHexString(md5.digest());
	}

	public static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
			sb.append(hexChar[b[i] & 0x0f]);
		}
		return sb.toString();
	}

}
